package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.Notice;
import com.meichis.yndms.repository.ApiUtil;

import java.util.List;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-04-14 22:51
 */
public class NoticeListManager extends BaseManager {

    private static NoticeListManager INSTANCE;

    public static NoticeListManager getInstance() {
        if (INSTANCE == null) {
            synchronized (NoticeListManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new NoticeListManager();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * @param showDialog
     * @param partnerId  MDS经销商ID
     * @param staffId    DMS员工id
     * @param data
     */
    public void GetNotice(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, String startDate, String endDate, String noticeType, MutableLiveData<List<Notice>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        hs.put("startDate", startDate);
        hs.put("endDate", endDate);
        hs.put("noticeType", noticeType);
        getRootApi().GetNotice(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<Notice>>() {
                }.getType(), new IWebServiceCallback<List<Notice>>() {
                    @Override
                    public void onSucceed(int sequence, List<Notice> resultPack, String ReturnInfo, int Return) {
                        data.setValue(resultPack);
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

    /**
     * @param showDialog
     * @param partnerId  MDS经销商ID
     * @param staffId    DMS员工id
     * @param data
     */
    public void GetNewProductNotice(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, String startDate, String endDate, MutableLiveData<List<Notice>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        hs.put("startDate", startDate);
        hs.put("endDate", endDate);
        getRootApi().GetNotice(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<Notice>>() {
                }.getType(), new IWebServiceCallback<List<Notice>>() {
                    @Override
                    public void onSucceed(int sequence, List<Notice> resultPack, String ReturnInfo, int Return) {
                        data.setValue(resultPack);
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }


    /**
     * @param showDialog
     * @param partnerId  MDS经销商ID
     * @param staffId    DMS员工id
     * @param data
     */
    public void UpdateNoticeRead(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, String infoId, MutableLiveData<String> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        hs.put("infoId", infoId);
        getRootApi().UpdateNoticeRead(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<String>() {
                }.getType(), new IWebServiceCallback<String>() {
                    @Override
                    public void onSucceed(int sequence, String resultPack, String ReturnInfo, int Return) {
                        data.setValue(resultPack);
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }

}
